package controller;

import dao.Impl.ClassDaoImpl;
import po.Class;
import po.Grade;
import service.ClassService;
import service.GradeService;
import service.serviceImpl.ClassServiceImpl;
import service.serviceImpl.GradeServiceImpl;
import util.ServletRepeat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ClassServlet",urlPatterns = "/ClassServlet")
public class ClassServlet extends BaseServlet  {

    private ClassService service=new ClassServiceImpl();
    public void queryAllClass(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s = request.getParameter("gid");
        int gid = Integer.parseInt(s);
        List<Class> classes = service.queryAllClass(gid);
        if(classes.size()>0){
            request.setAttribute("gradeId",gid);
            request.setAttribute("classes",classes);
            request.getRequestDispatcher("/allClass.jsp").forward(request,response);
        }else{
            request.setAttribute("gradeId",gid);
            request.setAttribute("img","暂时无相关班级哟！");
            request.getRequestDispatcher("/emptyClass.jsp").forward(request,response);
        }
    }
    public void addClass(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String s = request.getParameter("gid");
        int gid = Integer.parseInt(s);
        Class aClass = new Class();
        aClass.setNamed(name);
        aClass.setGid(gid);
        service.insert(aClass);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }
    public void updateClass(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String g = request.getParameter("gid");
        int gid = Integer.parseInt(g);
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        String name = request.getParameter("name");
        Class aClass = new Class();
        aClass.setId(id);
        aClass.setGid(gid);
        aClass.setNamed(name);
        service.update(aClass);
        boolean result2=true;
        String json2 = ServletRepeat.getJson2(result2);
        response.getWriter().write(json2);
    }

    public void queryOneClass(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("id");
        int id = Integer.parseInt(c);
        Class aClass = service.queryOne(id);
        request.setAttribute("classes",aClass);
        request.getRequestDispatcher("/updateClass.jsp").forward(request,response);

    }
    public void deleteClass(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        Class aClass = service.queryOne(id);
        Integer gid = aClass.getGid();
        service.delete(aClass);
        List<Class> classes = service.queryAllClass(gid);
        if(classes.size()>0){
            System.out.println(11);
            request.setAttribute("gradeId",gid);
            request.setAttribute("classes",classes);
            request.getRequestDispatcher("/allClass.jsp").forward(request,response);
        }else{
            request.setAttribute("gradeId",gid);
            request.setAttribute("img","暂时无相关班级哟！");
            request.getRequestDispatcher("/emptyClass.jsp").forward(request,response);
        }

    }














}
